package my_web.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import Proxy.DynamicProxy;
import ReqBean.RequestBean;
import my_web.Interface.UserInterface;
import my_web.bean.Emp;
import my_web.bean.User;
import my_web.service.UserService;
public class LogServlet extends HttpServlet{
	private boolean JudgeEmpSession(String key) {
		Map<String,HttpSession> map = Emp.map;
		/**
		 * 说明：
		 * Map.Entry<String, HttpSession>的意思是一个泛型，表示Entry里装的是分别是String类型和HttpSession,分别对应map的key和value
		 * map.entrySet():将map里的每一个键值对取出来封装成一个Entry对象在存到一个Set集合里 
		 */
		for (Map.Entry<String, HttpSession> entry : map.entrySet()) {
			if (key.equals(entry.getKey())) {
				return true;
			}
		}
		return false;	
	}
	protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		res.setContentType("text/html;charset=UTF-8 ");
		String path=req.getServletPath();
		UserInterface  service = null;
		try {
			service=DynamicProxy.creatServiceProxy(UserInterface.class, UserService.class);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		HttpSession session=req.getSession();
		if("/DoLog.do".equals(path)){
			try {
				User user=RequestBean.getBeanFromRequest(User.class, req);
				Boolean b=service.checkUser(user);
				if(b==false){//用户不存在，则重新转发到登录页
					res.getWriter().print("<script  type='text/javascript'>alert('工号或密码错误，请重新输入');window.location.href='login.jsp';</script>");
				}else if(b==true){
					Emp emp=service.queryEmpById(user);
					String e_id=emp.getE_id();
					if(JudgeEmpSession(e_id)){
						Emp.map.get(e_id).invalidate();//session失效
						Emp.map.remove(e_id);//从Map集合中移除原键值对
					}
					session.setAttribute("Emp", emp);
					res.getWriter().print("<script  type='text/javascript'>alert('登陆成功');window.location.href='home.jsp';</script>");
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else if("/LogToOut.do".equals(path)){
			res.setContentType("appliaction/json;utf-8");
			PrintWriter out=res.getWriter();
			out.print("{\"success\":true,\"msg\":\"退出成功！\"}");
			session.invalidate();
			out.flush();
			out.close();
			//res.sendRedirect("login.jsp");
		}else if("/LogToUpd.do".equals(path)){
			req.getRequestDispatcher("PwtUpd.jsp").forward(req, res);	
		}else if("/LogDoUpd.do".equals(path)){
			try {
				Emp emp=(Emp) session.getAttribute("Emp");
				String password=emp.getPassword();
				String pwt1=req.getParameter("pwt1");//输入的原密码
				String pwt2=req.getParameter("pwt2");//新密码1
				String pwt3=req.getParameter("pwt3");//新密码2
				if(pwt1.length()!=0 && pwt2.length()!=0 && pwt3.length()!=0){//原密码，新密码不为空
					if(pwt1.equals(password)){//输入的原密码和数据库密码相等
						if(pwt2.equals(pwt3)){//两次输入的新密码一致
							String name=((Emp) session.getAttribute("Emp")).getE_id();
							service.modifyPwt(pwt2,name);
							req.getRequestDispatcher("login.jsp").forward(req, res);
						}
					}
				}
			}catch (Exception e) {
				e.printStackTrace();
			}	
		}
	}
	protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		this.doGet(req, res);
	}
}